﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.CoreDomain.Business;
using Actuals.Business;
using System.Collections;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;


namespace Actuals
{
    public partial class AddTechnicalTasks : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            //ClientScript.GetPostBackEventReference(this, string.Empty);
            string empId;
            if (Session["EmployeeId"] == null)
            {

                Response.Write("Please login!");
                Response.End();
            }
            empId = Session["EmployeeId"].ToString();



            if (!Page.IsPostBack)
            {
              
                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IList<IEmployee> employees = businessEmployee.GetEmployees();
                ddlTaskCompletedBy.DataSource = employees;
                ddlTaskCompletedBy.DataTextField = "NickName";
                ddlTaskCompletedBy.DataValueField = "Id";
                ddlTaskCompletedBy.DataBind();
                ddlTaskCompletedBy.Items.Insert(0, addSelect());

                ddlCodeRevDoneBy.DataSource = employees;
                ddlCodeRevDoneBy.DataTextField = "NickName";
                ddlCodeRevDoneBy.DataValueField = "Id";
                ddlCodeRevDoneBy.DataBind();
                ddlCodeRevDoneBy.Items.Insert(0, addSelect());





             

                if (Request.QueryString["Mode"] != null)
                {
                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;

                    string _userStoryId = Request.QueryString["UserStoryId"].ToString();
                    ViewState["UserStoryId"] = _userStoryId;

                    IBusinessUserStory businessUserStory = new BusinessUserStory();
                    IUserStory userStory = businessUserStory.GetUserStory(int.Parse(_userStoryId));

                    IBusinessTask businessTask = new BusinessTask();
                    ITask task = businessTask.GetTask(userStory.TaskId);

                    int _projectId = task.ProjectId;
                    int _releaseId = task.ReleaseId;
                    ViewState["ProjectId"] = _projectId;
                    ViewState["ReleaseId"] = _releaseId;

                    if (_mode == "Edit")
                    {
                        if (Request.QueryString["Form"] != null)
                        {
                            string _form = Request.QueryString["Form"].ToString();
                            ViewState["Form"] = _form;
                            

                        }
                        ddlUserStory.Enabled = true;
                                              

                        //string _userStoryId = Request.QueryString["UserStoryId"].ToString();
                        //ViewState["UserStoryId"] = _userStoryId;
                        string _technicalTaskId = Request.QueryString["TechnicalTaskId"].ToString();
                        hidTechnicalTaskId.Value = _technicalTaskId;

                       // IBusinessUserStory businessUserStory = new BusinessUserStory();
                        //IUserStory userStory = businessUserStory.GetUserStory(int.Parse(_userStoryId));

                        string _taskId = string.Empty;
                        if (Request.QueryString["TaskId"] == null)
                            _taskId = userStory.TaskId.ToString();
                        else
                            _taskId = Request.QueryString["TaskId"].ToString();

                        ViewState["TaskId"] = _taskId;

                        PopulateTechnicalTask(int.Parse(_technicalTaskId));
                    }
                    else
                    {

                        txtEstimatedTime.Text = "0";
                        txtRemainingTime.Text = "0";
                        txtTimeSpent.Text = "0";
                        txtTotalElapsedTime.Text = "0";

                      
                        // taskId = int.Parse(_taskId);
                    }

                    IBusinessSprint businessSprint = new BusinessSprint();
                    IList<ISprint> sprints = businessSprint.GetSprints(int.Parse(ViewState["ProjectId"].ToString()),
                                                               int.Parse(ViewState["ReleaseId"].ToString()));
                    ddlSprintId.DataSource = sprints;
                    ddlSprintId.DataTextField = "SprintDescription";
                    ddlSprintId.DataValueField = "SprintId";
                    ddlSprintId.DataBind();
                    ddlSprintId.Items.Insert(0, "Select");


                }


            }


        }

       protected ListItem addSelect()
        {
            ListItem li = new ListItem();
            li.Value = "0";
            li.Text = "Select";
            return li;
        }

        protected void PopulateTechnicalTask(int technicalTaskId)
        {
            IBusinessTechnicalTask businessTechnicalTask = new BusinessTechnicalTask();
            ITechnicalTask technicalTasks = businessTechnicalTask.GetTechnicalTask(technicalTaskId);
            //if (ViewState["Form"].ToString() == "ShowSprintDetails")
            //{
            //    ddlCodeRevDoneBy.Enabled = false;
            //    ddlCodeRevStatus.Enabled = false;
            //}
            //if (technicalTasks.Status == "I" && technicalTasks.CodeReviewStatus == "T" && (Session["EmployeeId"].ToString()) != technicalTasks.TaskCompletedBy)
            //{
            //    txtUserStoryDescription.Enabled = false;
            //    ddlStatus.Enabled = false;
            //    ddlTaskCompletedBy.Enabled = false;
            //    ddlCodeRevStatus.Enabled = false;
            //    ddlCodeRevDoneBy.Enabled = false;
            //    ddlSprintId.Enabled = false;
            //}
            //else if (technicalTasks.Status == "C" && technicalTasks.CodeReviewStatus == "T")
            //{
            //    txtUserStoryDescription.Enabled = false;
            //    ddlSprintId.Enabled = false;
            //    if ((Session["EmployeeId"].ToString()) != technicalTasks.TaskCompletedBy)
            //    {
            //        ddlStatus.Enabled = false;
            //        ddlTaskCompletedBy.Enabled = false;
            //        ddlCodeRevStatus.Enabled = true;
            //        ddlCodeRevDoneBy.Enabled = true;

            //    }
            //    else
            //    {
            //        ddlStatus.Enabled = true;
            //        ddlTaskCompletedBy.Enabled = true;
            //        ddlCodeRevStatus.Enabled = false;
            //        ddlCodeRevDoneBy.Enabled = false;
            //    }

            //}
            //else if (technicalTasks.Status == "C" && technicalTasks.CodeReviewStatus == "I")
            //{
            //    txtUserStoryDescription.Enabled = false;
            //    if ((Session["EmployeeId"].ToString()) == technicalTasks.TaskCompletedBy)
            //    {
            //        ddlStatus.Enabled = true;
            //    }
            //    else
            //    {
            //        ddlStatus.Enabled = false;
            //    }
            //    if ((Session["EmployeeId"].ToString())== technicalTasks.CodeReviewDoneBy)
            //    {
            //        ddlCodeRevDoneBy.Enabled = true;
            //    }
            //    else
            //    {
            //        ddlCodeRevDoneBy.Enabled = false;
            //    }
            //    ddlTaskCompletedBy.Enabled = false;
            //    ddlCodeRevStatus.Enabled = false;
                
            //    ddlSprintId.Enabled = false;

            //}
            txtUserStoryDescription.Text = technicalTasks.UserStoryDescription;
            ddlSprintId.SelectedValue = technicalTasks.SprintId.ToString();
            ddlStatus.SelectedValue = technicalTasks.Status.ToString();
            ddlTaskCompletedBy.SelectedValue = technicalTasks.TaskCompletedBy.ToString();
            ddlCodeRevStatus.SelectedValue = technicalTasks.CodeReviewStatus.ToString();
            ddlCodeRevDoneBy.SelectedValue = technicalTasks.CodeReviewDoneBy.ToString();
            txtEstimatedTime.Text = technicalTasks.EstimatedTime.ToString();
            txtRemainingTime.Text = technicalTasks.RemainingTime.ToString();
            txtTotalElapsedTime.Text = technicalTasks.TotalElapsedTime.ToString();
            txtTimeSpent.Text = "0";
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IList<IUserStory> userStories = businessUserStory.GetUserStories(int.Parse(ViewState["TaskId"].ToString()));
            ddlUserStory.DataSource = userStories;
            ddlUserStory.DataTextField = "UserStoryDescription";
            ddlUserStory.DataValueField = "UserStoryId";
            ddlUserStory.DataBind();
            ddlUserStory.SelectedValue = ViewState["UserStoryId"].ToString();
        }



        public void btnSave_Click(Object sender, EventArgs e)
        {
            try
            {

                ITechnicalTask technicalTask = new TechnicalTask();
                // technicalTask.UserStoryId = int.Parse( ViewState["UserStoryId"].ToString()) ; 
                technicalTask.UserStoryDescription = txtUserStoryDescription.Text;
                technicalTask.SprintId = int.Parse(ddlSprintId.SelectedValue);
                technicalTask.Status = ddlStatus.SelectedValue;
                technicalTask.TaskCompletedBy = ddlTaskCompletedBy.SelectedValue;
                technicalTask.CodeReviewStatus = ddlCodeRevStatus.SelectedValue;
                technicalTask.CodeReviewDoneBy = ddlCodeRevDoneBy.SelectedValue;
                technicalTask.EstimatedTime = int.Parse(txtEstimatedTime.Text);
                technicalTask.RemainingTime = int.Parse(txtRemainingTime.Text);
                


                

                IBusinessTechnicalTask businesstechnicalTask = new BusinessTechnicalTask();
                if (ViewState["Mode"].ToString() == "Edit")
                {
                    technicalTask.TechnicalTaskId = int.Parse(hidTechnicalTaskId.Value);
                    technicalTask.UserStoryId = int.Parse(ddlUserStory.SelectedValue.ToString());
                    technicalTask.TimeSpent = int.Parse(txtTimeSpent.Text);
                    technicalTask.TotalElapsedTime = int.Parse(txtTotalElapsedTime.Text);
                    businesstechnicalTask.UpdateTechnicalTask(technicalTask);

                    lblMsg.Text = "Updated";


                }
                else if (ViewState["Mode"].ToString() == "Add")
                {

                    technicalTask.UserStoryId = int.Parse(ViewState["UserStoryId"].ToString());
                    businesstechnicalTask.AddTechnicalTask(technicalTask);
                    lblMsg.Text = "Saved";
                }
            }
            catch (Exception ex)
            {

                lblMsg.Text = ex.Message;
            }

        }









    }
}




